package com.star.wellconnect.signuplogin;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.star.wellconnect.R;
import com.star.wellconnect.sqlite.AppPreferences;
import com.star.wellconnect.sqlite.Patient;

public class SignupFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment_signup, container, false);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState){
		super.onViewCreated(view, savedInstanceState);
		
		final EditText username = (EditText) view.findViewById(R.id.username);
		final EditText password = (EditText) view.findViewById(R.id.password);
		final EditText name = (EditText) view.findViewById(R.id.name);
		final EditText email = (EditText) view.findViewById(R.id.email);
		final EditText phone = (EditText) view.findViewById(R.id.phone);
		Button signup = (Button) view.findViewById(R.id.signup);
		final TextView error = (TextView) view.findViewById(R.id.error);
		
		// Listener
		TextWatcher inputFieldsValidatorTextWatcher = new TextWatcher(){
			@Override
			public void afterTextChanged(Editable arg0) {}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				error.setText("");
			}
		};
		
		username.addTextChangedListener(inputFieldsValidatorTextWatcher);
		password.addTextChangedListener(inputFieldsValidatorTextWatcher);
		name.addTextChangedListener(inputFieldsValidatorTextWatcher);
		phone.addTextChangedListener(inputFieldsValidatorTextWatcher);
		email.addTextChangedListener(inputFieldsValidatorTextWatcher);

		signup.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(isNotEmpty()){					
					Patient patient = new Patient(username.getText().toString().trim(),
							"0",
							name.getText().toString().trim(),
							email.getText().toString().trim(),
							phone.getText().toString().trim(),
							null);
					
					String pass = password.getText().toString().trim();
					
					if(!AppPreferences.usernameTaken(getActivity(), patient.username)){
						if(AppPreferences.addPatient(getActivity(), patient, pass) >= 0){
			                error.setText("");
			                SignupLoginActivity activity = (SignupLoginActivity) getActivity();
			                activity.switchPage(0);
						}else{
							error.setText(getResources().getString(R.string.error_occured));
						}
					}else{
						error.setText(R.string.username_taken);
					}
				}else{
					error.setText(getResources().getString(R.string.fill_required_field));
				}
			}
			
			private boolean isNotEmpty(){
				return username.getText().toString().trim().length() > 0
						&& password.getText().toString().trim().length() > 0
						&& email.getText().toString().trim().length() > 0
						&& phone.getText().toString().trim().length() > 0
						&& name.getText().toString().trim().length() > 0;
			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
}
